package com.apes.fa.service;

import com.apes.fa.model.FaCard;
import com.apes.fa.model.FaInventoryLossCard;
import com.apes.fa.model.FaInventoryProcessTable;
import com.apes.fa.respository.FaCardRepository;
import com.apes.fa.respository.FaInventoryLossCardRepository;
import com.apes.fa.respository.FaInventoryProcessTableRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("faInventoryProcessTableService")
public class FaInventoryProcessTableService extends DomainService {

    @Autowired
    private FaInventoryProcessTableRepository faInventoryProcessTableRepository;
    @Autowired
    private FaInventoryLossCardRepository faInventoryLossCardRepository;
    @Autowired
    private FaCardRepository faCardRepository;
    @Autowired
    private FaCardAccountService faCardAccountService;


    /**
     * 保存与修改
     */
    private FaInventoryProcessTable save(SimpleRequest request){

        FaInventoryProcessTable faInventoryProcessTable = request.getO(FaInventoryProcessTable.class);
        return faInventoryProcessTableRepository.saveAndFlush(faInventoryProcessTable);

    }

    /**
     * 审核生成盘亏卡片账
     */
    private FaInventoryProcessTable approve(SimpleRequest request){

        FaInventoryProcessTable faInventoryProcessTable = request.getO(FaInventoryProcessTable.class);
        faInventoryProcessTable.setBusinessState("approve");
        faInventoryProcessTable.setApproveDate(new Date());
        faInventoryProcessTable.setApproveUid(this.getPerson(request));

        List<FaInventoryLossCard> faInventoryLossCards = faInventoryProcessTable.getFaInventoryLossCards();
        for (FaInventoryLossCard faInventoryLossCard : faInventoryLossCards) {
            Optional<FaCard> optional = faCardRepository.findById(faInventoryLossCard.getFaCard().getId());
            FaCard faCard = optional.get();
            faCardAccountService.inventoryLoss(faCard,request);
        }

        return faInventoryProcessTableRepository.saveAndFlush(faInventoryProcessTable);

    }


    /**
     * 根据卡片id查全部
     */
    public List<FaInventoryLossCard> findCard(SimpleRequest request){

        String id = request.get("id");
        List<FaInventoryLossCard> faInventoryLossCards = faInventoryLossCardRepository.findAll(JpaDsl.toCriteriaByEq("faCardId",id));
        //自定义排序 返回-1 排序到最前面
        Collections.sort(faInventoryLossCards, new Comparator<FaInventoryLossCard>() {
            @Override
            public int compare(FaInventoryLossCard o1, FaInventoryLossCard o2) {
                if(Objects.isNull(o1.getFaInventoryProcessTable().getApproveDate()) || Objects.isNull(o2.getFaInventoryProcessTable().getApproveDate())){
                    return -1;
                }else{
                    return o1.getFaInventoryProcessTable().getApproveDate().compareTo(o2.getFaInventoryProcessTable().getApproveDate());
                }
            }
        });
        return faInventoryLossCards;

    }

}
